package net.csdn.bigc2001.lockmyphone;

import android.app.Activity;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.util.Locale;

public class MainActivity extends Activity {
    private static final String tag = "+>MainActivity";
    private DevicePolicyManager policyManager;
    private SharedPreferences preferences;
    public static final String FLAG = "flag.enable";
    private boolean needDeviceManager = false;
    private TextView tvMemo;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(tag, "--onCreate");
        policyManager = (DevicePolicyManager) MainActivity.this.getSystemService(Context.DEVICE_POLICY_SERVICE);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        if ("1".contains(preferences.getString(FLAG, "0"))) {
            Log.d(tag, "已正常运行，不需要显示提示界面了");
            this.setTheme(android.R.style.Theme_NoDisplay);
            lockMyPhone();
            return;
        }
        needDeviceManager = true;
        Log.d(tag, "不能正常锁屏，需要提示用户去设置权限");
        setContentView(R.layout.activity_main);
        this.tvMemo = this.findViewById(R.id.tvMemo);
        this.imageView = this.findViewById(R.id.imgPic);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(tag, "--onStart 厂商：" + Build.BRAND + ", 型号：" + android.os.Build.MODEL);
        if (needDeviceManager) requestDeviceManager();
        String brand = Build.BRAND;
        if (brand == null) brand = "";
        else brand = brand.toLowerCase(Locale.ROOT);
        if (brand.contains("vivo")) {//针对不同设备做个性化提醒，这里就不继续做了，意思一下
            this.tvMemo.setText("应用需要权限才能使用，您可以到“设置-更多设置-安全-设备管理器”中，激活本应用的管理权限，如下图所示：");
            this.imageView.setImageResource(R.drawable.dev_manager_vivo);
        } else {
            this.tvMemo.setText("应用需要权限才能使用，您需要找到您手机的“设备管理器”功能，激活本应用的管理权限，类似下图所示：");
            this.imageView.setImageResource(R.drawable.dev_manager_vivo);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(tag, "--onResume");
    }

    private void lockMyPhone() {
        try {
            policyManager.lockNow();
            preferences.edit().putString(FLAG, "1").apply();
            MainActivity.this.finish();
        } catch (SecurityException e) {
            Log.e(tag, "未获得权限");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(tag, "--onActivityResult =" + resultCode);
    }

    private void requestDeviceManager() {
        Log.d(tag, "准备请求管理器权限");
        ComponentName componentName = new ComponentName(MainActivity.this, LockPhoneReceiver.class);
        if (policyManager == null) {
            Log.d(tag, "无法获得设备安全管理器");
            return;
        }
        //使用隐式意图调用系统方法来激活指定的设备管理器
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "一键锁屏");
        this.startActivityForResult(intent, 0);
    }

    private static class LockPhoneReceiver extends DeviceAdminReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            super.onReceive(context, intent);
            Log.d(tag, "onreceiver");
        }

        @Override
        public void onEnabled(Context context, Intent intent) {
            super.onEnabled(context, intent);
            Log.d(tag, "激活使用");
        }

        @Override
        public void onDisabled(Context context, Intent intent) {
            super.onDisabled(context, intent);
            Log.d(tag, "取消激活");
        }
    }
}